package cn.gogoyc.order.service.impl;

import cn.gogoyc.order.domain.OrderPetAcquisition;
import cn.gogoyc.order.mapper.OrderPetAcquisitionMapper;
import cn.gogoyc.order.query.OrderPetAcquisitionQuery;
import cn.gogoyc.order.service.IOrderPetAcquisitionService;
import cn.gogoyc.org.basic.servie.impl.BaseServiceImpl;
import cn.gogoyc.org.basic.util.LoginContext;
import cn.gogoyc.org.basic.util.PageList;
import cn.gogoyc.org.domain.Employee;
import cn.gogoyc.org.domain.Shop;
import cn.gogoyc.org.mapper.EmployeeMapper;
import cn.gogoyc.org.mapper.ShopMapper;
import cn.gogoyc.pet.domain.Pet;
import cn.gogoyc.pet.domain.SearchMasterMsg;
import cn.gogoyc.pet.dto.OrderDto;
import cn.gogoyc.pet.mapper.PetMapper;
import cn.gogoyc.pet.mapper.SearchMasterMsgMapper;
import cn.gogoyc.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * OrderPetAcquisitionServiceImpl业务实现类:
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;


    /**
     * 重载方法,动态SQL拼接,判断登录用户的身份
     * @param query
     * @param request
     * @return
     */
    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        /*判断店长还是店员*/
        //工具类获取的当前登陆人的信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //根据关联字段当前登陆人在后台的身份
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //如果他不等于null则是有管理店铺
        if (employee.getShop_id()!=null){
            //根据员工管理的店铺id去查询店铺对象
            Shop shop = shopMapper.loadById(employee.getShop_id());
            //如果店铺管理员字段不和员工的id关联的话则是店员,给query设置值到xml中动态查询
            if (shop.getAdminId() != employee.getId()){
                query.setEmpId(employee.getId());
            }
            //如果店铺管理员id和员工id匹配的话则是店长,将该店铺id设置到xml中动态查询
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    /**
     * 订单取消
     * @param id
     */
    @Override
    public void cancel(Long id) {
        //查询当前订单对象
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //取消时将该订单对应的寻主信息,状态设为3(放入宠物池),shopId置空 无任何关联
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //该订单的状态设为-1(取消)
        order.setState(-1);
        //更新订单信息
        orderPetAcquisitionMapper.update(order);
    }

    /**
     * 订单确认
     * @param orderDto
     */
    @Override
    public void confirm(OrderDto orderDto) {
        //当前订单id
        Long id = orderDto.getId();
        //价格
        BigDecimal money = orderDto.getMoney();
        //支付类型
        Integer payType = orderDto.getPayType();

        //根据当前订单id去查询自己方便关联其他对象
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //因为订单确认宠物也要存库中,查询对应的寻主信息,只有寻主信息才有详细的信息进行宠物入库
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        //宠物设置好值后同步入库
        Pet pet = createPet(msg,money);
        petMapper.save(pet);

        //订单状态1(成功)
        order.setState(1);
        //设置自定义价格
        order.setPrice(money);
        //关联入库好的宠物对象
        order.setPetId(pet.getId());
        //关联好后更新当前订单的信息
        orderPetAcquisitionMapper.update(order);
        //订单已经完成了,寻主信息也要改状态为完成。
        msg.setState(2);
        //更新寻主信息
        searchMasterMsgMapper.update(msg);
    }

    private Pet createPet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        //根据寻主信息名同步宠物名
        pet.setName(msg.getName());
        //成本价格,前端传来的
        pet.setCostprice(money);
        //出售价格,前端传来的
        pet.setSaleprice(money);
        //寻主信息的类型同步宠物类型
        pet.setTypeId(msg.getPetType());
        //寻主信息的图片同步宠物图片
        pet.setResources(msg.getResources());
        //关联收买宠物的店铺
        pet.setShopId(msg.getShopId());
        //关联寻主信息,该值可证明此宠物是收买来的
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
